﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace LIVITECH_ModbusDevLib
{

    public class RectScanDev
    {
        private Thread thread = null;
        private ScanDevConsole.ScanDeviceProcess devProcess = new ScanDevConsole.ScanDeviceProcess();
        public ScanDevConsole.ScanDeviceProcess.ScanDevParamsSZ param = new ScanDevConsole.ScanDeviceProcess.ScanDevParamsSZ();

        public RectScanDev(string portname = "COM1", int baudrate = 19200, int slaveID = 1)
        {
            devProcess.portname = portname;
            devProcess.baudrate = baudrate;
            devProcess.slaveID = slaveID;

        }

        ~RectScanDev()
        {
            CloseDevice();
            devProcess = null;
        }

        //打开设备
        public int OpenDevice(out string result)
        {      
            result = "";
            if (thread != null) return 0;

            thread = new Thread(devProcess.ScanDevRun);
            thread.Start();
            Thread.Sleep(3000);

            if (!thread.IsAlive)
            {
                result = "打开设备串口失败";
                return -1;
            }

            RedLaserOnOff(false);
            return 0;

        }

        //关闭设备
        public void CloseDevice()
        {
            if (thread == null || devProcess == null) return;

            if (thread.IsAlive)
            {
                RedLaserOnOff(false);  //关闭辅助红光
                Thread.Sleep(200);

                param.probeType = 0;
                param.scanShape = 0;
                param.x_start_v = 0;
                param.x_end_v = 0;
                param.y_start_v = 0;
                param.y_end_v = 0;
                devProcess.cmdApplyScanParams = true;
                Thread.Sleep(200);
            }

            devProcess.reqQuitRun = true;
            int i = 10;

            while (thread.IsAlive)
            {
                Thread.Sleep(200);
                i--;
                if (i <= 0)
                {
                    thread.Abort();
                    break;
                }
            }

            thread = null;
            devProcess.reqQuitRun = false;
        }

        //返回设备是否通信有效
        public bool IsDeviceValid
        {
            get
            {
                return !devProcess.IsCommFailed;
            }
        }

        // 打开或关 闭辅助红光
        public void RedLaserOnOff(bool on)
        {
            if(thread != null && thread.IsAlive)
            {
                devProcess.redLightOn = on;
                devProcess.cmdRedLight = true;
            }
        }

        // 复位振镜模式时的波形
        public void ResetRectPos()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.cmdResetRectPos = true;
            }
        }

        //参数值刷新到设备中
        public void UpdateParamsToDev()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.scanParams = param;
                devProcess.cmdApplyScanParams = true;
            }
        }

        //获取探头类型检测输入口状态
        public bool ProbeTypeSignal
        {
            get
            {
                return devProcess.input_ProbeType;
            }
        }

        //获取拍照按钮状态
        public bool CaptureButtonSignal
        {
            get
            {
                return devProcess.input_Capture;
            }
        }

        //获取录像按钮状态
        public bool VideoCapButtonSignal
        {
            get
            {
                return devProcess.input_VideoCap;
            }
        }

    }

}
